package com.neusoft.yiyang.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.yiyang.common.entity.medical.DrugDict;
import com.neusoft.yiyang.common.entity.medical.MedicationOrder;
import com.neusoft.yiyang.common.entity.medical.MedicationOrderRecord;
import com.neusoft.yiyang.medical.mapper.DrugDictMapper;
import com.neusoft.yiyang.medical.mapper.MedicationOrderMapper;
import com.neusoft.yiyang.medical.mapper.MedicationOrderRecordMapper;
import com.neusoft.yiyang.medical.service.MedicationOrderRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MedicationOrderRecordServiceImpl implements MedicationOrderRecordService {

    @Autowired
    private MedicationOrderRecordMapper medicationOrderRecordMapper;

    @Autowired
    private MedicationOrderMapper medicationOrderMapper;

    @Autowired
    private DrugDictMapper drugDictMapper;

    @Override
    public void createMedicationOrderRecord(MedicationOrderRecord medicationOrderRecord) {
        medicationOrderRecordMapper.insert(medicationOrderRecord);
    }

    @Override
    public List<MedicationOrderRecord> getAllMedicationOrderRecords() {
        return medicationOrderRecordMapper.selectList(null);
    }

    @Override
    public List<MedicationOrderRecord> getMedicationOrderRecordsByCustomerId(Integer customerId) {
        return medicationOrderRecordMapper.selectListByCustomerId(customerId);
    }

    @Override
    public void updateMedicationOrderRecord(MedicationOrderRecord medicationOrderRecord) {
        medicationOrderRecordMapper.updateById(medicationOrderRecord);
    }

    @Override
    public void deleteMedicationOrderRecord(Integer medicationOrderRecordId) {
        medicationOrderRecordMapper.deleteById(medicationOrderRecordId);
    }

    //利用CustomerId查询药品使用记录
    @Override
    public List<Map<String, Object>> getMedicationRecordByCustomerId(Integer customerId){
        List<Map<String, Object>> result = new ArrayList<>();
        List<MedicationOrderRecord> medicationOrderRecords = medicationOrderRecordMapper.selectListByCustomerId(customerId);
        for (MedicationOrderRecord record : medicationOrderRecords) {
            MedicationOrder medicationOrder = medicationOrderMapper.selectById(record.getMedicationOrderId());
            DrugDict drugDict = drugDictMapper.selectById(medicationOrder.getDrugId());
            Map<String, Object> map = new HashMap<>();
            map.put("medicationOrderRecords", record);
            map.put("drugDicts", drugDict);
            result.add(map);
        }
        return result;
    }

    //利用medicationOrderId查询对应药品使用记录
    @Override
    public List<MedicationOrderRecord> getMedicationRecordByMedicationOrderId(Integer medicationOrderId) {
        QueryWrapper<MedicationOrderRecord> qw = new QueryWrapper<>();
        qw.eq("medication_order_id", medicationOrderId);
        return medicationOrderRecordMapper.selectList(qw);
    }
}
